<?php

namespace App\Http\Controllers\Api;

use App\Http\Models\Contract;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 * 劳务协议
 *
 * @Author hechengwei
 * @email chichiooo@sina.com
 * @DateTime 2022-12-23
 */
class ContractController extends BaseController
{
    /**
     * 劳务协议列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-12-23
     * @param Request $request
     * @return void
     */
    public function contractList(Request $request)
    {
        $rules = [
            'page' => ['required'],
            'limit' => ['required'],
        ];

        $message = [
            'page.required' => '页码不能为空',
            'limit.required' => '限制条数不能为空',
        ];

        $payload = $request->only('page', 'limit');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $where['user_id'] = memberId();
        $model = new Contract();
        $list = $model->getList((int) $payload['page'], (int) $payload['limit'], $where, ['id', 'medical_id',  'name', 'status', 'status as status_text', 'created_at', 'audit_status', 'audit_status as audit_status_text', 'audit_remarks', 'url', 'is_pay', 'pay_time']); // 20240412新增, 'is_pay', 'pay_time' 支付状态、支付时间
        return success($list);
    }


    /**
     * 上传协议
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-12-26
     * @param Request $request
     * @return void
     */
    public function contractUpload(Request $request)
    {
        $rules = [
            // 'id' => ['required'],
            'url' => ['required'],
            'file_name' => ['required'],
            'medical_id' => ['required'],
        ];

        $message = [
            // 'id.required' => 'Id不能为空',
            'url.required' => '请上传协议',
            'file_name.required' => '请填写协议名称',
            'medical_id.required' => '请选择病例',
        ];

        $payload = $request->only('id', 'medical_id', 'url', 'file_name');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }
        if (Contract::upload($payload)) {
            return success();
        } else {
            return fail_message();
        }
    }

    /**
     * 下载协议
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-12-26
     * @param Request $request
     * @return void
     */
    public function contractDownload(Request $request)
    {
        $rules = [
            'id' => ['required'],
        ];

        $message = [
            'id.required' => 'id不能为空',
        ];

        $payload = $request->only('id');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $url = Contract::getContract($payload['id']);
        if ($url) {
            return success($url);
        } else {
            return fail_message('未找到合同');
        }
    }


    /**
     * 查看协议
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-12-26
     * @param Request $request
     * @return void
     */
    public function contractCheck(Request $request)
    {
        $rules = [
            'id' => ['required'],
        ];

        $message = [
            'id.required' => 'id不能为空',
        ];

        $payload = $request->only('id');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $url = Contract::getContract($payload['id']);
        if ($url) {
            return success($url);
        } else {
            return fail_message('未找到合同');
        }
    }

    /**
     * 删除协议
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2023-04-11
     * @param Request $request
     * @return void
     */
    public function contractDelete(Request $request)
    {
        $rules = [
            'id' => ['required'],
        ];

        $message = [
            'id.required' => 'id不能为空',
        ];

        $payload = $request->only('id');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $url = Contract::destroy($payload['id']);
        if ($url) {
            return success();
        } else {
            return fail_message('未找到合同');
        }
    }
}
